<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="../include/admincontent.jsp" %>
<div id="Content">
    <form id="modelform" method="post" action="/BaseEvaluation/EditAction">
        <input type="hidden" name="id" value="${table.id}" datatype="*" nullmsg="非法访问"/>
        <div class="toolbars">
            <a id="save">
                <em class="ico16 save_traft_16"></em>
                <span class="menu_span" title="保存">保存</span>
            </a>
            <a id="add">
                <em class="ico16 add_new"></em>
                <span class="menu_span" title="添加">添加</span>
            </a>
        </div>
        <table class="treeTable inputform">
            <thead>
            <tr>
                <td colspan="2">评价表信息</td>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td width="100">评价表名称:</td>
                <td>
                    <div><input type="text" name="name" value="${table.name}" datatype="*" nullmsg="评价表名称不能为空！" style="line-height: 38px;height: 38px;padding: 0 5px;width: calc(100% - 5px);border: 0;"/></div>
                </td>
            </tr>
            </tbody>
        </table>
        <table class="treeTable inputform" style="margin-top:10x;" id="gusertb" >
            <thead>
            <tr>
                <td colspan="8">评价项目信息</td>
            </tr>
            <tr>
                <td rowspan="2" width="100">评价要素</td>
                <td rowspan="2">观察要点</td>
                <td rowspan="2" width="60">分数</td>
                <td colspan="4">评分标准</td>
                <td rowspan="2" class="tdwidth"></td>
            </tr>
            <tr>
                <td width="60">好</td>
                <td width="60">较好</td>
                <td width="60">一般</td>
                <td width="60">差</td>
            </tr>
            </thead>
            <tbody>
            <c:forEach var="_item" items="${items}" varStatus="item">
                <tr>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].name" value="${_item.name}"
                                    datatype="*" nullmsg="评价要素不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].remark" value="${_item.remark}"
                                    datatype="*" nullmsg="观察要点不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].allpoint" value="${_item.allpoint}"
                                    datatype="zzzs" nullmsg="分数不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].level1" value="${_item.level1}"
                                    datatype="*" nullmsg="评分标准不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].level2" value="${_item.level2}"
                                    datatype="*" nullmsg="评分标准不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].level3" value="${_item.level3}"
                                    datatype="*" nullmsg="评分标准不能为空"/></div>
                    </td>
                    <td>
                        <div><input type="text" name="evaluationItems[${item.index}].level4" value="${_item.level4}"
                                    datatype="*" nullmsg="评分标准不能为空"/></div>
                    </td>
                    <c:choose>
                        <c:when test="${item.index==0}">
                            <td class="tdwidth">
                                <%--<div>
                                    <button type="button" class="add"></button>
                                </div>--%>
                            </td>
                        </c:when>
                        <c:otherwise>
                            <td class="tdwidth">
                                     <button type="button"  class="layui-btn layui-btn-sm layui-btn-normal del"><i class="layui-icon"></i>删除</button>
                            </td>
                        </c:otherwise>
                    </c:choose>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </form>
</div>
<script type="text/javascript">
    $(document).ready(function () {
       /* //添加
        $(document).delegate('#gusertb .add', 'click', function () {
            var str = $(this).parent().parent().parent().html().replace('class="add"', 'class="del"');
            $('#gusertb tbody').append('<tr>' + str + '</tr>');
            setItemNum();
        });*/
        $("#add").click(function () {
            $('#gusertb tbody').append(' <tr>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].name" datatype="*" nullmsg="评价要素不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].remark" datatype="*" nullmsg="观察要点不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].allpoint" datatype="zzzs" nullmsg="分数不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].level1" datatype="*" nullmsg="评分标准不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].level2" datatype="*" nullmsg="评分标准不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].level3" datatype="*" nullmsg="评分标准不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td>\n' +
                '                    <div><input type="text" name="evaluationItems[0].level4" datatype="*" nullmsg="评分标准不能为空"/></div>\n' +
                '                </td>\n' +
                '                <td class="tdwidth">\n' +
                '                    <button type="button"  class="layui-btn layui-btn-sm layui-btn-normal del"><i class="layui-icon"></i>删除</button>\n' +
                '                </td>\n' +
                '            </tr>');
            setItemNum();
        });
        //删除
        $(document).delegate('#gusertb .del', 'click', function () {
            $(this).parent().parent().remove();
            setItemNum();
        });

        $('#save').click(function () {
            var rows = gusertb.rows.length;
            var number = 0;
            var data = {};
            data["name"] = $("input[name='name']").val();
            for (var i = 0; i < rows - 3; i++) {
                var evaName = $("input[name='evaluationItems[" + i + "].name']").val()
                var evaNameName = "evaluationItems[" + i + "].name";
                data[evaNameName] = evaName;
                if (evaName == ""){
                    layer.msg("评价要素不能为空", {icon: 2});
                    return;
                }

                var evaRemark = $("input[name='evaluationItems[" + i + "].remark']").val();
                var evaRemarkName = "evaluationItems[" + i + "].remark";
                data[evaRemarkName] = evaRemark;
                if (evaRemark == ""){
                    layer.msg("观察要点不能为空", {icon: 2});
                    return;
                }


                var evaAllpoint = $("input[name='evaluationItems[" + i + "].allpoint']").val();
                var evaAllpointName = "evaluationItems[" + i + "].allpoint";
                data[evaAllpointName] = evaAllpoint;
                if (evaAllpoint == ""){
                    layer.msg("评价分数不能为空", {icon: 2});
                    return;
                }
                number += parseInt(evaAllpoint);

                var evaLevel1 = $("input[name='evaluationItems[" + i + "].level1']").val();
                var evaLevel1Name = "evaluationItems[" + i + "].level1";
                data[evaLevel1Name] = evaLevel1;

                var evaLevel2 = $("input[name='evaluationItems[" + i + "].level2']").val();
                var evaLevel2Name = "evaluationItems[" + i + "].level2";
                data[evaLevel2Name] = evaLevel2;

                var evaLevel3 = $("input[name='evaluationItems[" + i + "].level3']").val();
                var evaLevel3Name = "evaluationItems[" + i + "].level3";
                data[evaLevel3Name] = evaLevel3;

                var evaLevel4 = $("input[name='evaluationItems[" + i + "].level4']").val();
                var evaLevel4Name = "evaluationItems[" + i + "].level4";
                data[evaLevel4Name] = evaLevel4;

                if (evaLevel1 == "" || evaLevel2 == "" || evaLevel3 == "" || evaLevel4 == ""){
                    layer.msg("评分标准不能为空", {icon: 2});
                    return;
                }

            }

            if (number != 100){
                layer.msg("分数之和必须为100", {icon: 2});
                return;
            }


            $.ajax({
                url: "/BaseEvaluation/EditAction",
                type: 'POST',
                data: data,
                success: function (data) {
                    layer.msg(data.Message, {icon: 1});
                    parent.location.reload();
                },
                error: function (data) {
                    layer.msg(data.Message, {icon: 2});
                }
            });

        });
    });

    function setItemNum() {
        $('#gusertb tbody tr').each(function (_index) {
            $(this).find('td:eq(0) input').attr('name', 'evaluationItems[' + _index + '].name');
            $(this).find('td:eq(1) input').attr('name', 'evaluationItems[' + _index + '].remark');
            $(this).find('td:eq(2) input').attr('name', 'evaluationItems[' + _index + '].allpoint');
            $(this).find('td:eq(3) input').attr('name', 'evaluationItems[' + _index + '].level1');
            $(this).find('td:eq(4) input').attr('name', 'evaluationItems[' + _index + '].level2');
            $(this).find('td:eq(5) input').attr('name', 'evaluationItems[' + _index + '].level3');
            $(this).find('td:eq(6) input').attr('name', 'evaluationItems[' + _index + '].level4');
        });
    }
</script>
</body>
</html>